import { Form, FormInstance } from 'antd';
import store from '@/store';
import { useEffect, useMemo } from 'react';
import { Print } from '@/type';

export interface UseFormProps {
    searchPrinterKey: string;
}

export default function useForm(
    options: UseFormProps
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
): [FormInstance<any>, Print[]] {
    const { app } = store.useStore(),
        [form] = Form.useForm();

    // 打印机列表数据
    const printerList = useMemo(() => {
        const printerList = app.printerList.result;
        return printerList.filter((item) =>
            item.value
                .toLocaleLowerCase()
                .includes(options.searchPrinterKey.toLocaleLowerCase())
        );
    }, [app.printerList.result, options.searchPrinterKey]);

    // 默认值设定
    useEffect(() => {
        if (app.defaultPrinter) {
            form.setFieldsValue({ printer: app.defaultPrinter });
        }
    }, [app.defaultPrinter]);

    return [form, printerList];
}
